package com.softer.chengxin.param.controller;

import com.github.pagehelper.PageInfo;
import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.login.service.impl.LoginServiceimpl;
import com.softer.chengxin.param.entity.ScoreType;
import com.softer.chengxin.param.entity.ScoreTypeDetail;
import com.softer.chengxin.param.service.ScoreTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2019/12/13
 */
@RestController
@RequestMapping("/api/scoreType")
public class ScoreTypeController {
    @Autowired
    private ScoreTypeService scoreTypeService;
    @Autowired
    private LoginServiceimpl loginService;

    /**
     * 新增评分类型
     *
     * @param list 类型集合
     * @return 新增结果
     */
    @PostMapping("/add")
    public CommonResult add(@RequestBody List<ScoreType> list, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return result;
        }
        return scoreTypeService.add(list);
    }

    /**
     * 删除评分类型
     *
     * @param id      ID
     * @param session 缓存
     * @return 删除结果
     */
    @PostMapping("/delete")
    public CommonResult delete(int id, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return result;
        }
        return scoreTypeService.delete(id);
    }

    /**
     * 删除分数类型
     *
     * @param id      ID
     * @param session 缓存
     * @return 删除结果
     */
    @PostMapping("/deleteScoreTypeDetail")
    public CommonResult deleteScoreTypeDetail(int id, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return result;
        }
        return scoreTypeService.deleteScoreTypeDetail(id);
    }

    /**
     * 修改评分类型
     *
     * @param scoreType 评分类型
     * @return 新增结果
     */
    @PostMapping("/update")
    public CommonResult update(@RequestBody ScoreType scoreType, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return result;
        }
        return scoreTypeService.update(scoreType);
    }

    /**
     * 查询评分类型详情
     *
     * @param id      类型ID
     * @param session session
     * @return 详情
     */
    @PostMapping("/getInfo")
    public ScoreType getInfo(int id, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return new ScoreType();
        }
        return scoreTypeService.getInfo(id);
    }

    /**
     * 查询评分类型列表
     *
     * @param scoreType 查询实体
     * @param page      分页实体
     * @param session   session
     * @return 评分类型列表
     */
    @PostMapping("/getScoreTypeList")
    public PageInfo<ScoreType> getScoreTypeList(ScoreType scoreType, Page page, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return new PageInfo<>();
        }
        return scoreTypeService.getScoreTypeList(scoreType, page);
    }

    /**
     * 修改评分类型状态
     *
     * @param id      评分类型ID
     * @param state   状态（0关闭，1开启）
     * @param session session
     * @return 修改结果
     */
    @PostMapping("/updateState")
    public CommonResult updateState(int id, int state, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return result;
        }
        return scoreTypeService.updateState(id, state);
    }

    /**
     * 获取评分类型下拉列表
     *
     * @param type    类型（1优良，2负面，3严重失信）
     * @param session session
     * @return 评分类型下拉列表
     */
    @PostMapping("/getTypeList")
    public List<ScoreType> getTypeList(int type, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return new ArrayList<>();
        }
        return scoreTypeService.getTypeList(type);
    }

    /**
     * 获取
     * 未关闭的
     * 评分类型下拉列表
     *
     * @param type 类型（1优良，2负面，3严重失信）
     * @return 评分类型下拉列表
     */
    @PostMapping("/getOpenTypeList")
    public List<ScoreType> getOpenTypeList(int type, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return new ArrayList<>();
        }
        return scoreTypeService.getOpenTypeList(type);
    }

    /**
     * 获取分数下拉列表
     *
     * @param id      评分类型ID
     * @param session session
     * @return 分数下拉列表
     */
    @PostMapping("/getScoreList")
    public List<ScoreTypeDetail> getScoreList(int id, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return new ArrayList<>();
        }
        return scoreTypeService.getScoreList(id);
    }

}
